<?php

define('VERSION','0.1');
			
$get_file = trim($_GET['file']);

if( !empty($get_file) && file_exists($get_file) && @fopen($get_file, "rb") ) {
	
	download($get_file);

}

function ls(){

	$self=basename(__FILE__);

	$get_dir=$_GET['dir'];
	$home=str_replace("\\","/",dirname(__FILE__));

	if(!empty($_GET['dir']) && @opendir($get_dir)) {
		$path=str_replace("\\","/",$get_dir);
	}else{
		$path=$home;
	}

	$up_lvl=str_replace("\\","/",dirname($path.".."));

	$dir=dir($path);

	$fname=array();
	$fdate=array();
	$fsize=array();
	$ftype=array();
	$fdata=array();

	while ($file = $dir->read()){

		if(($file!=$self)&&($file!=".")&&($file!="..")){

			array_push($ftype, filetype($path.'/'.$file));
			if(filetype($path.'/'.$file)=="file"){
				array_push($fdata, substr(strrchr($file, "."), 1));
			}else{
				array_push($fdata, "dir");
			}
			array_push($fname, $file);
			array_push($fdate, date("d/m/Y H:i", filemtime($path.'/'.$file)));
			array_push($fsize, filesize($path.'/'.$file));

		}
	}

	$dir->close();

	array_multisort($ftype,$fdata,$fname,$fdate,$fsize);

	echo '<table>';
	echo '<th width=50px></th>';
	echo '<th width=500px></th>';
	echo '<th width=200px></th>';
	echo '<th width=200px></th>';

	echo '<tr><td align=center><a href='.$self.'?dir='.$up_lvl.'><img src="css/icons/up.png"></a></td>';

	for($i=0; $i<count($fname); $i++){

		echo '<tr>';

		if( file_exists( "css/icons/".$fdata[$i].".png") ) {
			echo '<td align=center><img src="css/icons/'.$fdata[$i].'.png"></td>';
		}else{
			echo '<td align=center><img src="css/icons/file.png"></td>';
		}

		if($ftype[$i]=="dir"){
			echo '<td>&nbsp;&nbsp<a  href='.$self.'?dir='.$path.'/'.str_ireplace(" ", "%20", $fname[$i]).'>'.$fname[$i].'</a></td>';
		}else{
			echo '<td>&nbsp;&nbsp<a target="_blank" href="'.$self.'?file='.$path.'/'.$fname[$i].'">'.$fname[$i].'</a></td>';
		}

		echo '<td align=center>'.file_size($fsize[$i]).'</td>';
		echo '<td align=center>'.$fdate[$i].'</td></tr>';

	}
	echo '</table>';

}

function file_size($size){

	$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
	return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '';

}

function download($file){
	
	header("Content-type: application/$ext");
	header("Content-Transfer-Encoding: Binary");
	header("Content-length: ".filesize($file));
	header("Content-disposition: attachment; filename=\"".basename($file)."\"");
	readfile($file);

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>php-fex v<?php echo VERSION ?></title>

<link rel="stylesheet" type="text/css" href="css/default.css">

<div id="header">
	<div id="logo">
		<h1><a href="<?php echo $self.'?dir=' ?>">php-fex</a></h1><br>
	</div>
</div>

<div id="content">

	<div id="colOne">
		<?php
			ls();
		?>
	</div>

	<div id="colTwo" align=center>	
	</div>

</div>

<div id="footer">
	<p><a target="_blank" href="http://code.google.com/p/php-fex/">http://code.google.com/p/php-fex/</p>
</div>